<template>
  <div class="collection">
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <div class="filter-container">
        <el-input
          v-model="queryInfo.query.name"
          placeholder="姓名"
          style="width: 100px;"
          class="filter-item"
          @keyup.enter.native="getList"
        />
        <el-date-picker
          v-model="date"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00']"
        >></el-date-picker>

        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="getList">Search</el-button>
        <el-button
          :loading="downloadLoading"
          type="primary"
          icon="el-icon-document"
          @click="handleDownload"
        >导出excel</el-button>
      </div>

      <div class="table-box">
        <el-table
          border
          stripe
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="expand">
            <template slot-scope="scope">
              <pre>{{ scope.row }}</pre>
            </template>
          </el-table-column>
          <!-- <el-table-column type="index"></el-table-column> -->

          <!-- <el-table-column type="selection" width="55" align="center"></el-table-column>
          <el-table-column prop="time" label="操作时间" width align="center"></el-table-column>
          <el-table-column prop="name" label="操作人" width align="center"></el-table-column>
          <el-table-column prop="ip" label="IP地址" width align="center"></el-table-column>
          <el-table-column prop="operation" label="操作" width align="center"></el-table-column>-->

          <!-- 演示数据 -->
          <el-table-column prop="dataName" label="商品" width align="center"></el-table-column>
          <el-table-column prop="creator" label="创建人" width align="center"></el-table-column>
          <el-table-column prop="createTime" label="创建时间" width align="center"></el-table-column>
          <el-table-column prop="changeTime" label="修改时间" width align="center"></el-table-column>
          <el-table-column label="操作" width="200" align="center">
            <template slot-scope="scope">
              <!-- {{scope.row}} -->
              <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="small"
                  @click="removeUserById(scope.row.dataName)"
                ></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" content="编辑" placement="top" :enterable="false">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  size="small"
                  @click="showEditDialog(scope.row.dataName)"
                ></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" content="查看" placement="top" :enterable="false">
                <el-button
                  type="success"
                  icon="el-icon-search"
                  size="small"
                  @click="handleOperate(scope.row)"
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Collection',
  data() {
    return {
      tableData: [
        {
          dataName: '商品1',
          creator: 'Admin',
          createTime: '2019-03-01 12:33:10',
          changeTime: '2019-03-02 15:00:06'
        },
        {
          dataName: '商品2',
          creator: 'Admin',
          createTime: '2019-02-03 13:35:09',
          changeTime: '2019-02-14 11:06:11'
        },
        {
          dataName: '商品3',
          creator: 'Admin',
          createTime: '2019-01-11 10:33:55',
          changeTime: '2019-02-02 12:00:03'
        },
        {
          dataName: '商品4',
          creator: 'Admin',
          createTime: '2019-01-01 22:55:44',
          changeTime: '2019-01-02 15:00:06'
        },
        {
          dataName: '商品5',
          creator: 'Admin',
          createTime: '2018-11-16 11:33:14',
          changeTime: '2018-12-02 15:55:06'
        },
        {
          dataName: '商品6',
          creator: 'Admin',
          createTime: '2018-10-21 11:33:32',
          changeTime: '2018-12-09 11:15:26'
        },
        {
          dataName: '商品7',
          creator: 'Admin',
          createTime: '2018-08-26 16:22:25',
          changeTime: '2018-12-02 15:44:23'
        },
        {
          dataName: '商品8',
          creator: 'Admin',
          createTime: '2018-08-23 10:43:10',
          changeTime: '2018-09-22 17:25:25'
        },
        {
          dataName: '商品9',
          creator: 'Admin',
          createTime: '2018-07-17 11:33:35',
          changeTime: '2018-11-02 15:55:23'
        },
        {
          dataName: '商品10',
          creator: 'Admin',
          createTime: '2018-05-19 11:34:35',
          changeTime: '2018-09-04 18:31:21'
        }
      ],
      // 时间
      date: [],
      // 获取用户列表的参数对象
      queryInfo: {
        query: {
          name: '',
          date: []
        },
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 10
      },
      // 日志列表
      loglist: [],
      total: 0,
      // 控制添加用户对话框的显示与隐藏
      addDialogVisible: false,
      list: null,
      multipleSelection: [],
      filename: '',
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 导出
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 下载
    handleDownload() {
      if (this.multipleSelection.length) {
        this.downloadLoading = true
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['操作时间', '操作人', 'IP地址', '操作']
          const filterVal = ['time', 'name', 'ip', 'operation']
          const list = this.multipleSelection
          const data = this.formatJson(filterVal, list)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: this.filename
          })
          this.$refs.multipleTable.clearSelection()
          this.downloadLoading = false
        })
      } else {
        this.$message({
          message: 'Please select at least one item',
          type: 'warning'
        })
      }
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    handleOperate(ele) {
      // console.log(ele)
      this.$router.push({
        path: '/tables/details'
      })
    },
    async getList() {
      this.$http.defaults.headers['IP'] = location.href.toString()
      this.$http.defaults.headers['token'] = window.sessionStorage.getItem(
        'token'
      )
      // const { data: res } = await this.$http.get('users', {
      //   params: this.queryInfo
      // })
      // if (res.meta.status !== 200) {
      //   return this.$message.error('获取用户列表失败！')
      // }
      // this.userlist = res.data.users
      // this.total = res.data.total
      // console.log(res)
      // this.userlist = this.tableData
      this.total = this.tableData.length
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getList()
    }
  }
}
</script>

<style lang="stylus">
.el-date-picker{
}
</style>
